package com.easytodo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.StringRequestListener;
import com.easytodo.entity.Item;
import com.easytodo.entity.Project;
import com.easytodo.ui.today.TodayFragment;
import com.easytodo.ui.todo.TodoFragment;

import java.util.Date;
import java.util.List;

public class ProjectAdapter extends BaseAdapter {
    Context context;
    List<Project> projects;
    LayoutInflater inflater;
    Fragment fragment;

    public final class ItemView{
        TextView projectName;
        TextView num;
        TextView rate;
        RelativeLayout bak;
    }

    public ProjectAdapter(Context context, List<Project> projects, Fragment fragment){
        this.context=context;
        this.projects=projects;
        inflater=LayoutInflater.from(context);
        this.fragment=fragment;
    }
    @Override
    public int getCount() {
        return projects.size();
    }

    @Override
    public Object getItem(int i) {
        return projects.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ItemView itemView=null;


        if(view==null){
            itemView=new ItemView();
            view=inflater.inflate(R.layout.project,null,false);

            itemView.projectName=view.findViewById(R.id.projectName);
            itemView.num=view.findViewById(R.id.num);
            itemView.rate=view.findViewById(R.id.rate);
            itemView.bak=view.findViewById(R.id.bak);

            view.setTag(itemView);
        }else {
            itemView = (ItemView) view.getTag();
        }

        itemView.projectName.setText(projects.get(i).getPath());
        itemView.num.setText(projects.get(i).getNum()+" todo in Total");
        Integer num=Integer.valueOf(projects.get(i).getNum());
        Integer fin=Integer.valueOf(projects.get(i).getFin());
        Double rate=fin*100.0/num;

        itemView.rate.setText(String.format("%.0f%%",rate));

        itemView.bak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context,ProjectItem.class);
                intent.putExtra("path",projects.get(i).getPath());
                fragment.getActivity().startActivity(intent);
            }
        });
        return view;
    }
}
